﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NapCardOnline.Models;
using PagedList;

namespace NapCardOnline.Controllers
{ 
    public class CommissionController : Controller
    {
        private NapCardOnlineEntities db = new NapCardOnlineEntities();

        //
        // GET: /Commission/

        public ViewResult Index()
        {
            User objUser = Session["User"] as User;
            var commissions = db.Commissions.Include("CommissionLevel").Include("CommissionType").Include("Cost").Include("Service").OrderByDescending(t => t.Service.ServiceName); ;
            return View(commissions);
        }

        //
        // GET: /Commission/Details/5

        public ViewResult Details(int id)
        {
            Commission commission = db.Commissions.Single(c => c.CommissionID == id);
            return View(commission);
        }

        //
        // GET: /Commission/Create

        public ActionResult Create()
        {
            ViewBag.FK_CommissionLevelID = new SelectList(db.CommissionLevels, "CommissionLevelID", "CommissionLevelName");
            ViewBag.FK_CommissionTypeID = new SelectList(db.CommissionTypes, "CommissionTypeID", "CommissionTypeName");
            ViewBag.FK_CostID = new SelectList(db.Costs, "CostID", "CostNo");
            ViewBag.FK_ServiceID = new SelectList(db.Services, "ServiceID", "ServiceNo");
            return View();
        } 

        //
        // POST: /Commission/Create

        [HttpPost]
        public ActionResult Create(Commission commission)
        {
            if (ModelState.IsValid)
            {
                db.Commissions.AddObject(commission);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.FK_CommissionLevelID = new SelectList(db.CommissionLevels, "CommissionLevelID", "CommissionLevelName", commission.FK_CommissionLevelID);
            ViewBag.FK_CommissionTypeID = new SelectList(db.CommissionTypes, "CommissionTypeID", "CommissionTypeName", commission.FK_CommissionTypeID);
            ViewBag.FK_CostID = new SelectList(db.Costs, "CostID", "CostNo", commission.FK_CostID);
            ViewBag.FK_ServiceID = new SelectList(db.Services, "ServiceID", "ServiceNo", commission.FK_ServiceID);
            return View(commission);
        }
        
        //
        // GET: /Commission/Edit/5
 
        public ActionResult Edit(int id)
        {
            Commission commission = db.Commissions.Single(c => c.CommissionID == id);
            ViewBag.FK_CommissionLevelID = new SelectList(db.CommissionLevels, "CommissionLevelID", "CommissionLevelName", commission.FK_CommissionLevelID);
            ViewBag.FK_CommissionTypeID = new SelectList(db.CommissionTypes, "CommissionTypeID", "CommissionTypeName", commission.FK_CommissionTypeID);
            ViewBag.FK_CostID = new SelectList(db.Costs, "CostID", "CostNo", commission.FK_CostID);
            ViewBag.FK_ServiceID = new SelectList(db.Services, "ServiceID", "ServiceNo", commission.FK_ServiceID);
            return View(commission);
        }

        //
        // POST: /Commission/Edit/5

        [HttpPost]
        public ActionResult Edit(Commission commission)
        {
            if (ModelState.IsValid)
            {
                db.Commissions.Attach(commission);
                db.ObjectStateManager.ChangeObjectState(commission, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.FK_CommissionLevelID = new SelectList(db.CommissionLevels, "CommissionLevelID", "CommissionLevelName", commission.FK_CommissionLevelID);
            ViewBag.FK_CommissionTypeID = new SelectList(db.CommissionTypes, "CommissionTypeID", "CommissionTypeName", commission.FK_CommissionTypeID);
            ViewBag.FK_CostID = new SelectList(db.Costs, "CostID", "CostNo", commission.FK_CostID);
            ViewBag.FK_ServiceID = new SelectList(db.Services, "ServiceID", "ServiceNo", commission.FK_ServiceID);
            return View(commission);
        }

        //
        // GET: /Commission/Delete/5
 
        public ActionResult Delete(int id)
        {
            Commission commission = db.Commissions.Single(c => c.CommissionID == id);
            return View(commission);
        }

        //
        // POST: /Commission/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Commission commission = db.Commissions.Single(c => c.CommissionID == id);
            db.Commissions.DeleteObject(commission);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}